//
//  XLGLoginCommonProcess.m
//  etionCBLogic
//
//  Created by wjxfhxy on 15-2-4.
//  Copyright (c) 2015年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGLoginCommonProcess.h"

#import "ProtocolBase.h"

#import "CGWService+ChannelProocol.h"

NSString *const XLGLoginCommonProcessLoginingCheckAccountStatusTip = @"验证帐号和密码⋯⋯";

NSString *const XLGLoginCommonProcessLoginingCheckVersionStatusTip = @"验证版本⋯⋯";

NSString *const XLGLoginCommonProcessLoginingGetUserInfoStatusTip = @"获取用户信息⋯⋯";

NSString *const XLGLoginCommonProcessLoginingGetEnterpriseInfoStatusTip = @"获取企业目录⋯⋯";

@implementation XLGLoginAccountInfo

-(void)dealloc
{
    [_mobile release_mm];
    [_account release_mm];
    [_externInfo release_mm];
    [_password release_mm];
    superdealloc_mm;
}

@end

@implementation XLGLoginCommonProcess

- (BOOL)checkPassword:(NSString *)userNumber password:(NSString *)password
{
    NSString *path = [NSString stringWithFormat:@"%@/%@/%@", [XMSFileHelper documentPath], userNumber, PSWF];
    return [[self localPassword:path] isEqualToString:password];
}

- (NSString *)localPassword:(NSString *)szPath
{
    NSData *data = [[NSFileManager defaultManager] contentsAtPath:szPath];
    int n = sizeof(char) * (int)data.length;
    char *buf = malloc(n);
    char *buffer = malloc(n / sizeof(int) + 1);
    [data getBytes:buf length:n];
    for (int i = 0; i < n; i += 4)
    {
        char f[4];
        memcpy(f, buf + i, 4);
        int ch = *(int *) f;
        ch /= 2;
        buffer[i / 4] = ch;
    }
    buffer[n / sizeof(int)] = '\0';
    NSString *psw = [NSString stringWithUTF8String:buffer];
    free(buf);
    free(buffer);
    return psw;
}

- (NSInteger)newlocalPassword:(NSString*)password
{
    [self savePasword:password];
    return YES;
}

- (void)savePasword:(NSString *)password
{
    NSString *path = [[XLGContext context].m_szUserpath stringByAppendingPathComponent:PSWF];
    const char *chPsw = [password UTF8String];
    size_t nLen = strlen(chPsw);
    NSMutableData *data = [[NSMutableData new] autorelease_mm];
    for (int i = 0; i < nLen; i++)
    {
        int ch = chPsw[i];
        ch *= 2;
        [data appendBytes:&ch length:sizeof(int)];
    }
    [[NSFileManager defaultManager] createFileAtPath:path contents:data attributes:nil];
}

@end
